package com.yowosoft.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.thoughtworks.xstream.XStream;
import com.yowosoft.model.Image;
import com.yowosoft.model.ImageMessage;
import com.yowosoft.model.Music;
import com.yowosoft.model.MusicMessage;
import com.yowosoft.model.News;
import com.yowosoft.model.NewsMessage;
import com.yowosoft.model.TextMessageType;

public class MessageUtils {
	/**
	 * 消息类型
	 */
	public static final String MESSAGE_TEXT = "text";
	public static final String MESSAGE_NEWS = "news";
	public static final String MESSAGE_IMAGE = "image";
	public static final String MESSAGE_VOICE = "voice";
	public static final String MESSAGE_MUSIC = "music";
	public static final String MESSAGE_VIDEO = "video";
	public static final String MESSAGE_LINK = "link";
	public static final String MESSAGE_LOCATION = "location";
	public static final String MESSAGE_EVENT = "event";
	public static final String MESSAGE_SUBSCRIBE = "subscribe";
	public static final String MESSAGE_UNSUBSCRIBE = "unsubscribe";
	public static final String MESSAGE_CLICK = "CLICK";
	public static final String MESSAGE_VIEW = "VIEW";

	/**
	 * 将xml文件转化为map对象
	 * 
	 * @param request
	 * @return
	 * @throws IOException
	 * @throws DocumentException
	 */
	@SuppressWarnings("unchecked")
	public static HashMap<String, String> xmlToMap(HttpServletRequest request) throws IOException, DocumentException {
		HashMap<String, String> map = new HashMap<String, String>();
		SAXReader reader = new SAXReader();
		InputStream inputStream = request.getInputStream();
		Document document = reader.read(inputStream);

		Element root = document.getRootElement();
		List<Element> list = root.elements();
		for (Element e : list) {
			map.put(e.getName(), e.getText());
		}
		inputStream.close();
		return map;
	}

	/**
	 * 将文本信息转化为xml
	 * 
	 * @param request
	 * @return
	 */
	public static String textMessageToXml(TextMessageType textMessageType) {
		XStream xStream = new XStream();
		xStream.alias("xml", textMessageType.getClass()); // 将文件的根节点转换为<xml></xml>
		return xStream.toXML(textMessageType);
	}

	public static String initText(String toUserName,String fromUserName,String content) {
		TextMessageType text = new TextMessageType();
		text.setFromUserName(toUserName);
		text.setToUserName(fromUserName);
		text.setMsgType(MESSAGE_TEXT);
		text.setCreateTime(new Date().getTime());
		text.setContent(content);
		return textMessageToXml(text);
	}

	/**
	 * 主菜单
	 * 
	 * @return
	 */
	public static String menuText() {
		StringBuffer sBuffer = new StringBuffer();
		sBuffer.append("欢迎您的关注，请按照菜单提示进行操作：\n\n");
		sBuffer.append("1.课程介绍\n");
		sBuffer.append("2.慕课网的介绍\n");
		sBuffer.append("3.图片测试\n");
		sBuffer.append("4.音乐测试\n");
		sBuffer.append("回复？调出此菜单。");
		return sBuffer.toString();
	}

	public static String firstMenu() {
		StringBuffer sBuffer = new StringBuffer();
		sBuffer.append("功能测试1");
		return sBuffer.toString();
	}
	
	public static String secondMenu() {
		StringBuffer sBuffer = new StringBuffer();
		sBuffer.append("功能测试2");
		return sBuffer.toString();
	}
	
	/**
	 * 将图文消息转化为xml
	 * 
	 * @param NewsMessage
	 * @return
	 */
	public static String newsMessageToXml(NewsMessage newsMessage) {
		XStream xStream = new XStream();
		xStream.alias("xml", newsMessage.getClass()); // 将文件的根节点转换为<xml></xml>
		xStream.alias("item", new News().getClass());
		return xStream.toXML(newsMessage);
	}
	/**
	 * 图文消息的组装
	 * @param toUserName
	 * @param fromUserName
	 * @return
	 */
	public static String initNewsMessage(String toUserName,String fromUserName){
		String  message=null;
		List<News> newsList=new ArrayList<News>();
		NewsMessage newsMessage = new NewsMessage();
		
		News news =new News();
		news.setTitle("图文消息测试1");
		news.setDescription("我的第一个公众平台");
		news.setPicUrl("http://114.215.128.92/weinxin/image/33.ico");
		news.setUrl("www.baidu.com");
		
		News news2 =new News();
		news2.setTitle("图文消息测试2");
		news2.setDescription("我的第一个公众平台");
		news2.setPicUrl("http://114.215.128.92/weixin/image/20.ico");
		news2.setUrl("www.baidu.com");
		
		News news3 =new News();
		news3.setTitle("图文消息测试3");
		news3.setDescription("我的第一个公众平台");
		news3.setPicUrl("http://114.215.128.92/weixin/image/1.jpeg");
		news3.setUrl("www.baidu.com");
		
		newsList.add(news3);
		newsList.add(news);
		newsList.add(news2);
		
		System.out.println(newsList.size()+newsList.get(0).getTitle()+newsList.get(1).getTitle()+newsList.get(2).getTitle());
		
		newsMessage.setToUserName(fromUserName);
		newsMessage.setFromUserName(toUserName);
		newsMessage.setCreateTime(new Date().getTime());
		newsMessage.setMsgType(MESSAGE_NEWS);
		newsMessage.setArticles(newsList);
		newsMessage.setArticleCount(newsList.size());
		message = newsMessageToXml(newsMessage);
		
		System.err.println(message);
		return message;
	}
	
	
	/**
	 * 图片消息转为xml
	 * @param imageMessage
	 * @return
	 */
	public static String imageMessageToXml(ImageMessage imageMessage){
		XStream xstream = new XStream();
		xstream.alias("xml", imageMessage.getClass());
		return xstream.toXML(imageMessage);
	}
	
	/**
	 * 组装图片消息
	 * @param toUserName
	 * @param fromUserName
	 * @return
	 */
	public static String initImageMessage(String toUserName,String fromUserName){
		String message = null;
		Image image = new Image();
		image.setMediaId("pJwxgPdV6tAf56oGr3oc6KW-X5g8RdoWux50crk4IqIKkqsW4md185ugRTjpbVj5");
		ImageMessage imageMessage = new ImageMessage();
		imageMessage.setFromUserName(toUserName);
		imageMessage.setToUserName(fromUserName);
		imageMessage.setMsgType(MESSAGE_IMAGE);
		imageMessage.setCreateTime(new Date().getTime());
		imageMessage.setImage(image);
		message = imageMessageToXml(imageMessage);
		System.out.println(message);
		return message;
	}

	/**
	 * 音乐消息转为xml
	 * @param musicMessage
	 * @return
	 */
	public static String musicMessageToXml(MusicMessage musicMessage){
		XStream xstream = new XStream();
		xstream.alias("xml", musicMessage.getClass());
		return xstream.toXML(musicMessage);
	}
	/**
	 * 组装音乐消息
	 * @param toUserName
	 * @param fromUserName
	 * @return
	 */
	public static String initMusicMessage(String toUserName,String fromUserName){
		String message = null;
		Music music = new Music();
		//music.setThumbMediaId("WsHCQr1ftJQwmGUGhCP8gZ13a77XVg5Ah_uHPHVEAQuRE5FEjn-DsZJzFZqZFeFk");
		music.setTitle("墓地");
		music.setDescription("墓地");
		music.setMusicUrl("http://hatcherzhang.ngrok.natapp.cn/yowosoft/mudi.mp3");
		music.setHQMusicUrl("http://hatcherzhang.ngrok.natapp.cn/yowosoft/mudi.mp3");
		
		MusicMessage musicMessage = new MusicMessage();
		musicMessage.setFromUserName(toUserName);
		musicMessage.setToUserName(fromUserName);
		musicMessage.setMsgType(MESSAGE_MUSIC);
		musicMessage.setCreateTime(new Date().getTime());
		musicMessage.setMusic(music);
		message = musicMessageToXml(musicMessage);
		System.out.println(message);
		return message;
	}
}
